package com.example.demo.directive;

import com.example.demo.dao.SysDicDao;
import com.example.demo.dao.SysDicItemDao;
import com.example.demo.domain.SysDic;
import com.example.demo.service.SysDicService;
import freemarker.core.Environment;
import freemarker.template.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Component
public class SelectDirective implements TemplateDirectiveModel {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private Configuration configuration;

    @Autowired
    private SysDicService sysDicService;

    @Autowired
    private SysDicDao sysDicDao;

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        String link = Optional.ofNullable(params.get("link")).orElse("").toString();
        String type = Optional.ofNullable(params.get("type")).orElse("").toString();
        String _default = Optional.ofNullable(params.get("default")).orElse("").toString();
        String name = Optional.ofNullable(params.get("name")).orElse("").toString();
        String required = Optional.ofNullable(params.get("required")).orElse("false").toString();


        if(StringUtils.isEmpty(link)){
            return;
        }

        if(StringUtils.isEmpty(type)){
            type = "select";
        }

        if(StringUtils.isEmpty(_default)){
            _default = "";
        }

        if(StringUtils.isEmpty(name)){
            name = "";
        }


        Map modelMap = new HashMap();
        modelMap.put("link", link);
        modelMap.put("type", type);
        modelMap.put("_default", _default);
        modelMap.put("name", name);
        modelMap.put("required", required);

        String[] split = link.split(",");

        switch (split.length){
            case 1:
                SysDic sysDic = sysDicService.getSysDic(split[0]);
                modelMap.put("options", sysDic.getItems());
                break;
            case 3:
                modelMap.put("options", sysDicDao.selectByTableName(split[0], split[1], split[2]));
                break;
            default:
                return;
        }

        Template template = configuration.getTemplate("sys/directive/selectDirective.ftl");
        String string = FreeMarkerTemplateUtils.processTemplateIntoString(template, modelMap);

        Writer out = env.getOut();

        if (body != null) {
            out.write(string);
            body.render(out);
        }else{
            out.write(string);
        }
        logger.trace("{}", "SelectDirective");
    }
}
